package org.apache.jsp.ForumHtml;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jsp.ForumHtml.notifier_jsp;
import clientData.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.Vector;
import java.rmi.Naming;
import exception.*;
import server.ForumHandlerInt;
import java.sql.*;
import java.rmi.Naming;
import server.ForumHandlerInt;

public final class posts_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.List<java.lang.String> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.ArrayList<java.lang.String>(2);
    _jspx_dependants.add("/ForumHtml/scripts.jsp");
    _jspx_dependants.add("/ForumHtml/serverConnection.jsp");
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.List<java.lang.String> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=utf-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			"", true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
      out.write("<head>\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n");
      out.write("<title>Posts</title>\r\n");
      out.write("<script src=\"SpryAssets/SpryMenuBar.js\" type=\"text/javascript\"></script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!-- this script is notifing the user about his friends -->\r\n");
      out.write("\r\n");
      out.write("<script type=\"text/javascript\" src=\"jquery.js\"></script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("\tfunction logoutFunc() {\r\n");
      out.write("\r\n");
      out.write("\t\tvar xmlhttp;\r\n");
      out.write("\r\n");
      out.write("\t\tif (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari\r\n");
      out.write("\t\t\txmlhttp = new XMLHttpRequest();\r\n");
      out.write("\t\t} else {// code for IE6, IE5\r\n");
      out.write("\t\t\txmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\r\n");
      out.write("\t\t}\r\n");
      out.write("\r\n");
      out.write("\t\txmlhttp.open(\"GET\", \"logout.jsp\", false);\r\n");
      out.write("\t\t//http.open(\"POST\", \"userValidation.jsp\", true);\r\n");
      out.write("\r\n");
      out.write("\t\txmlhttp.send();\r\n");
      out.write("\t\treturn xmlhttp.responseText;\r\n");
      out.write("\t}\r\n");
      out.write("</script>");
      out.write("\r\n");
      out.write("\r\n");
      out.write("  \r\n");
      out.write("  \r\n");
      out.write(" \r\n");
      out.write("<link href=\"SpryAssets/SpryMenuBarVertical.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("<link href=\"screenStyle.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("</head>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");

	
   String serverDomain = "localhost";
   ForumHandlerInt forum = null;
  
     try
     {
   	 forum =  (ForumHandlerInt)Naming.lookup("rmi://"+serverDomain+"/ForumService");
	   }
   catch (Exception e){
	   
	   
	   response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server.");
	   response.flushBuffer();
		return;
   }

      out.write("\r\n");
      out.write("\r\n");
      out.write('\r');
      out.write('\n');


	int topicId = Integer.parseInt(request.getParameter("topicId"));
	
	session.setAttribute("topicId",topicId);
	String _topicName = request.getParameter("topicName");
	
	
    if(request.getParameter("forumId")!=null) 
    	session.setAttribute("forumId",Integer.parseInt(request.getParameter("forumId")));

	if (session.getAttribute("userName")==null || session.getAttribute("forumId")==null)
	{
		response.sendRedirect("loggin.jsp");
		response.flushBuffer();
		return;
	}




      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<body>\r\n");
      out.write("\r\n");
      out.write("\t\r\n");
      out.write("\t\t<div class=\"pageTitle\">\r\n");
      out.write("\t\t<h1>POSTS OF TOPIC:");
      out.print(_topicName);
      out.write("</h1>\r\n");
      out.write("\t\t<hr />\r\n");
      out.write("\t</div>\r\n");
      out.write("\t\r\n");
      out.write("\t\r\n");
      out.write("\r\n");
      out.write("<ul id=\"MenuBar1\" class=\"MenuBarVertical\">\r\n");
      out.write("      <li><a href=\"friends.jsp\">manage friends</a></li>\r\n");
      out.write("      <li><a href=\"mainScreen.jsp\"> main screen</a>  </li>\r\n");
      out.write("      <li><a href=\"topics.jsp?forumId=");
      out.print((Integer)session.getAttribute("forumId"));
      out.write("&forumName=");
      out.print((String)session.getAttribute("forumName"));
      out.write("\"> forum: ");
      out.print((String)session.getAttribute("forumName"));
      out.write("</a>  </li>\r\n");
      out.write("  \t  <li><a href=\"addPost.jsp?topicId=");
      out.print(topicId);
      out.write("&topicName=");
      out.print(_topicName);
      out.write("\"> add Post</a>  </li>\r\n");
      out.write("        <li><a href=\"index.jsp\"  target=\"_top\" onClick= \"logoutFunc();\">logout</a>  </li>\r\n");
      out.write("</ul>\r\n");
      out.write("   \r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("    var MenuBar1 = new Spry.Widget.MenuBar(\"MenuBar1\", {imgRight:\"SpryAssets/SpryMenuBarRightHover.gif\"});\r\n");
      out.write("    </script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("    \r\n");
      out.write("<table border=2 align=\"center\">\r\n");


LinkedList<PostDataInt> posts = new LinkedList<PostDataInt>();

try
{
	 posts = forum.getPostList((String)session.getAttribute("userName"),(String)session.getAttribute("password"),topicId,(Integer)session.getAttribute("forumId"));
}
catch(NotLoggedInException t)
{

	response.sendRedirect("loggin.jsp");
	response.flushBuffer();
	return;
	
}
catch(Exception e){
			
	response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server."+e+
														(String)session.getAttribute("userName")+" "+
														(String)session.getAttribute("password")+" "+
														topicId+" "+
														session.getAttribute("forumId"));
	response.flushBuffer();
	return;
}


      out.write("\r\n");
      out.write("<tr>\r\n");
      out.write("<th>TITLE</th>\r\n");
      out.write("<th>CONTENT</th>\r\n");
      out.write("<th>CREATOR</th>\r\n");
      out.write("<th>CREATED</th>\r\n");
      out.write("<th>MODIFIED</th>\r\n");
      out.write("</tr>\r\n");

	
    for ( PostDataInt pd:posts ) {
        
      out.write("\r\n");
      out.write("        \r\n");
      out.write("        <tr id=");
      out.print(pd.getId());
      out.write(">\r\n");
      out.write("        \t\r\n");
      out.write("        \t<td id=title");
      out.print(pd.getId());
      out.write('>');
      out.print( pd.getTitle());
      out.write("</td>\r\n");
      out.write("        \r\n");
      out.write("        \t\r\n");
      out.write("\t\r\n");
      out.write("        \t<td id=content");
      out.print(pd.getId());
      out.write("> <textarea readonly=\"readonly\">");
      out.print(pd.getContent());
      out.write("</textarea></td>\r\n");
      out.write("\t\t\t\r\n");
      out.write("\t\t\t<td id=creator");
      out.print(pd.getId());
      out.write('>');
      out.print( pd.getCreator().getUserName());
      out.write("</td>\r\n");
      out.write("        \r\n");
      out.write("        \t<td id=creation");
      out.print(pd.getId());
      out.write('>');
      out.print( pd.getCreationTime().toLocaleString());
      out.write("</td>\r\n");
      out.write("        \t\r\n");
      out.write("        \t<td id=editing");
      out.print(pd.getId());
      out.write('>');
      out.print( pd.getEditingTime().toLocaleString());
      out.write("</td>\r\n");
      out.write("        \t");

				if ((((String)session.getAttribute("permission")).equals("ADMIN"))||(pd.getCreator().getUserName().equals(((String)session.getAttribute("userName"))))){
			
      out.write("\r\n");
      out.write("        \t<td id=edit");
      out.print(pd.getId());
      out.write("><input type=button onClick=\"procLink('");
      out.print(pd.getId());
      out.write("');\" value='edit post'><br/>\r\n");
      out.write("        \t<input type=button onClick=\"removePost('");
      out.print(pd.getId());
      out.write("');\" value='delete'></input></td>\r\n");
      out.write("        \t");

				}
        	
      out.write("\r\n");
      out.write("\t\t</tr>\r\n");
      out.write("        \r\n");
      out.write("        \r\n");
      out.write("        \r\n");
      out.write("       \r\n");
      out.write("        ");

    }

      out.write("\r\n");
      out.write("</table>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<form name=\"foo\" method=post action=\"editPost.jsp\">\r\n");
      out.write("\t\r\n");
      out.write("\t<input type=\"hidden\" name=\"postId\" value=\"\">\r\n");
      out.write("\t<input type=\"hidden\" name=\"postTitle\" value=\"\">\r\n");
      out.write("\t<input type=\"hidden\" name=\"postContent\" value=\"\">\r\n");
      out.write("\t<input type=\"hidden\" name=\"topicId\" value=\"\">\r\n");
      out.write("\t<input type=\"hidden\" name=\"topicName\" value=\"\">\r\n");
      out.write("\r\n");
      out.write("</form>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<script language=\"Javascript\">\r\n");
      out.write("  function procLink(pId) {\r\n");
      out.write("\t  \r\n");
      out.write("\t \r\n");
      out.write("    document.foo.postId.value = pId\r\n");
      out.write("    \r\n");
      out.write("    document.foo.postTitle.value = $(\"#title\"+pId).html()\r\n");
      out.write("    \r\n");
      out.write("    document.foo.postContent.value = $(\"#content\"+pId+\" textarea\").html()\r\n");
      out.write("    \r\n");
      out.write("    document.foo.topicId.value = ");
      out.print(request.getParameter("topicId"));
      out.write("\r\n");
      out.write("    \r\n");
      out.write("    document.foo.topicName.value = \"");
      out.print(request.getParameter("topicName"));
      out.write("\"\r\n");
      out.write("    \r\n");
      out.write("    document.foo.submit()\r\n");
      out.write("}\r\n");
      out.write("  \r\n");
      out.write("  \r\n");
      out.write("function removePost(pId)\r\n");
      out.write("{\r\n");
      out.write("\t$.ajax({\r\n");
      out.write("\t\ttype : \"GET\",\r\n");
      out.write("\t\turl : \"deletePost.jsp?postId=\"+pId\r\n");
      out.write("\t});\t\r\n");
      out.write("\t\r\n");
      out.write("\t$(\"#\"+pId).remove();\r\n");
      out.write("}\r\n");
      out.write("  </script>\r\n");
      out.write("\r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("</script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
